{{- define "descheduler_resources" }}
cpu: 25m
memory: 50Mi
{{- end }}

{{- range $d := .Values.descheduler.internal.deschedulers }}
  {{- $deschedulerSpec := $d.spec }}
  {{- if ($.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: descheduler-{{ $d.metadata.name | sha1sum }}
  namespace: d8-descheduler
  {{- include "helm_lib_module_labels" (list $ (dict "app" "descheduler")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: descheduler-{{ $d.metadata.name | sha1sum }}
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "descheduler"
      minAllowed:
        {{- include "descheduler_resources" $ | nindent 8 }}
      maxAllowed:
        cpu: 50m
        memory: 100Mi
  {{- end }}
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: descheduler-{{ $d.metadata.name | sha1sum }}
  namespace: d8-descheduler
  {{- include "helm_lib_module_labels" (list $ (dict "app" "descheduler")) | nindent 2 }}
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      app: descheduler-{{ $d.metadata.name }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: descheduler-{{ $d.metadata.name | sha1sum }}
  namespace: d8-descheduler
  {{- include "helm_lib_module_labels" (list $ (dict "app" "descheduler" "name" $d.metadata.name)) | nindent 2 }}
spec:
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: descheduler
      name: {{ $d.metadata.name }}
  template:
    metadata:
      labels:
        app: descheduler
        name: {{ $d.metadata.name }}
      annotations:
        checksum/config: {{ $deschedulerSpec | toJson | sha256sum }}
    spec:
      serviceAccountName: descheduler
      imagePullSecrets:
      - name: deckhouse-registry
      {{- include "helm_lib_node_selector" (tuple $ "system") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple $ "system") | nindent 6 }}
      {{- include "helm_lib_priority_class" (tuple $ "cluster-low") | nindent 6 }}
      {{ include "helm_lib_module_pod_security_context_run_as_user_deckhouse" . | nindent 6 }}
      containers:
      - name: descheduler
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" $ | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list $ "descheduler") }}
        volumeMounts:
        - mountPath: /policy
          name: policy-volume
        args:
        - "--policy-config-file"
        - "/policy/policy.yaml"
        - "--logging-format"
        - "json"
        - "--v"
        - "6"
        - "--descheduling-interval"
        - "15m"
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" $ | nindent 12 }}
{{- if not ($.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "descheduler_resources" $ | nindent 12 }}
{{- end }}
      volumes:
      - name: policy-volume
        configMap:
          name: descheduler-policy-{{ $d.metadata.name | sha1sum }}
{{- end }}
